package com.yunhe.authority.repository.company;

import com.yunhe.authority.domain.company.Company;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CompanyCrudRepository extends Neo4jRepository<Company,Long> {

    @Query("match(s:Station)-[:HAS_RESOURCE{type:'Company'}]->(c:Company) where ID(s)={stationId} and c.title={title} return c limit 1")
    Company findByStationAndTitle(@Param("stationId") Long stationId, @Param("title")String title);

    @Query("match(s:Station)-[r]->(c:Company{code:{code}}) delete r")
    void unbindStationByCode(@Param("code")Long code);

    @Query("match(c:Company) where c.unifiedSocialCreditCode = {unifiedSocialCreditCode} return c limit 1")
    Company findByUnifiedSocialCreditCode(@Param("unifiedSocialCreditCode") String unifiedSocialCreditCode);

    @Query("match(c:Company) return c order by c.leaseTime limit {pageSize} skip {page}")
    Iterable<Company> findAllForPage(@Param("page") int page , @Param("pageSize") int pageSize );

    @Query("MATCH (n:Company) set n.staffCount = toInteger(n.staffCount) return n")
    void castStaffCount();

    Company findByName(String name);

    @Query("match(c:Company) where c.code = {code} return c limit 1")
    Company findByCode(@Param("code")Long code);

    @Query("MATCH (c:Company)-[:HAS_OFFICE]->(o:Office) where ID(o) = {id} RETURN c limit 1")
    Company findByOfficeId(@Param("id")Long id);

    /**
     * 根据真实园区id查询企业列表
     * @param id 真实园区ID
     */
    @Query(value = "match(rt:RealStation)-[r:HAS_COMPANIES{realStationId:{id}}]->(c:Company) return c",
    countQuery = "match(rt:RealStation)-[r:HAS_COMPANIES{realStationId:{id}}]->(c:Company) return count(c)")
    Page<Company> findCompaniesByRealStationId(@Param("id") Long id, Pageable pageable);

    /**
     * 根据总园区id查询企业列表
     * @param id 真实园区ID
     */
    @Query(value = "match(s:Station)<-[:BELONG_TO_STATION{stationId:{id}}]-(rt:RealStation)-[r:HAS_COMPANIES{realStationId:id(rt)}]->(c:Company) return c",
    countQuery = "match(s:Station)<-[:BELONG_TO_STATION{stationId:{id}}]-(rt:RealStation)-[r:HAS_COMPANIES{realStationId:id(rt)}]->(c:Company)  return count(c)")
    Page<Company> findCompaniesByStationId(@Param("id") Long id, Pageable pageable);

    @Query(value = "match(rt:RealStation)-[r:HAS_COMPANIES{realStationId:{id}}]->(c:Company) where ID(rt)={id} return c order by c.sn asc,c.createTime asc")
    Iterable<Company> findByRealStationId(@Param("id") Long realStationId);

    @Query(value = "match(rt:RealStation)-[r:HAS_COMPANIES{realStationId:{id}}]->(c:Company{companyStatus:{companyStatus}}) where ID(rt)={id} return c order by c.sn asc,c.createTime asc")
    Iterable<Company> findByRealStationIdAndStatus(@Param("id") Long id,@Param("companyStatus") String type);

    @Query(value = "match(rt:RealStation)-[r:HAS_COMPANIES{realStationId:{id}}]->(c:Company) where ID(rt)={id} and c.title={title} return c limit 1")
    Company findByRealStationIdAndTitle(@Param("id") Long realStationId, @Param("title") String title);

    /**
     * 根据企业ID与真实园区ID查询企业
     * @param realStationId 企业ID
     * @param companyId 真实园区ID
     * @return Company
     */
    @Query(value = "match(rt:RealStation)-[r:HAS_COMPANIES{realStationId:{realStationId}}]->(c:Company) where ID(rt)={realStationId} and ID(c)={companyId} return c limit 1")
    Company findByRealStationIdAndCompanyId(@Param("realStationId") Long realStationId, @Param("companyId") Long companyId);

    List<Company> findByCompanyStatus(String companyStatus);

    /**
     * 根据员工ID查询企业
     * @param employeeId 员工ID
     * @return Company
     */
    @Query(value = "match(e:Employee)-[:HAS_COMPANY]->(c:Company) where ID(e)={employeeId} return c limit 1")
    Company findByEmployeeId(@Param("employeeId") Long employeeId);

    @Query(value = "match(rt:RealStation)-[r:HAS_COMPANIES{realStationId:{realStationId}}]->(c:Company) where ID(rt)={realStationId} and c.companyStatus = '0' return c order by c.sn asc,c.createTime asc")
    List<Company> findCompanyByRealStationId(@Param("realStationId") Long realStationId);
}
